package com.dai.zhenfujin.my.activity;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.ProListBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.ZXingUtils;
import com.dai.zhenfujin.view.RoundImageView;
import com.hyphenate.easeui.utils.SPUtils;

import java.util.HashMap;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import de.hdodenhof.circleimageview.CircleImageView;


/**
 * 推广赚钱
 */
public class GeneralizeActivity extends BaseActivity {

    private TextView tvTitle,generailze_name;
    private ImageView imageView,iv_qr;
    private CircleImageView iv_head;
    private Button setting_share;

    @Override
    protected void onCreate(Bundle savedInstanceState) { //user_name
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generalize);

        tvTitle = findViewById(R.id.title_textview);
        tvTitle.setText("分享赚钱");

        imageView = findViewById(R.id.title_imageview);
        generailze_name = findViewById(R.id.generailze_name);
        iv_head = (CircleImageView)findViewById(R.id.iv_head);
        iv_qr = findViewById(R.id.iv_qr);
        String userPic = SPUtils.getConversionHeadPhoto(this, SPUtils.getString(this,Constants.USER_MOBILE));
        Glide.with(this).load(userPic).into(iv_head);
        generailze_name.setText(getIntent().getStringExtra("user_name"));
        setting_share = findViewById(R.id.setting_share);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Map<String, Object> params5 = new HashMap<>();
        Map<String, String> params6 = new HashMap<>();
        params5.put("req_bd", params6);
        HttpClient.post(GeneralizeActivity.this, Constants.SHARE, params5, new CallBack<ProListBean>() {
            @Override
            public void onSuccess(ProListBean resultBean) {
                String url = resultBean.share_url;
                Bitmap bitmap =  ZXingUtils.createQRImage(url,iv_qr.getWidth(), iv_qr.getHeight());
                iv_qr.setImageBitmap(bitmap);
            }
        });

        initData();
    }

    private void initData() {
        setting_share.setOnClickListener(v -> showDialog());
    }

    private void showDialog() {
        setBackgroundAlpha((float) 0.4);
        View view = LayoutInflater.from(GeneralizeActivity.this).inflate(R.layout.dialog_share_item, null);
        PopupWindow  mPopWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(view);

        // 设置PopupWindow的背景
        mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setTouchable(true);
        view.findViewById(R.id.ll_weixin).setOnClickListener(v -> {
            Map<String, Object> params = new HashMap<>();
            Map<String, String> params2 = new HashMap<>();
            params.put("req_bd", params2);
            HttpClient.post(GeneralizeActivity.this, Constants.SHARE, params, new CallBack<ProListBean>() {
                @Override
                public void onSuccess(ProListBean resultBean) {
                    String url =  resultBean.share_url;
                    Platform platform = ShareSDK.getPlatform(Wechat.NAME);
                    Platform.ShareParams shareParams = new  Platform.ShareParams();
                    shareParams.setTitle(resultBean.share_title);
                    shareParams.setText(resultBean.share_desc);
                    shareParams.setImageUrl(resultBean.share_icon);
                    shareParams.setUrl(url);
                    //分享网页
                    shareParams.setShareType(Platform.SHARE_WEBPAGE);
                    platform.share(shareParams);
                }
            });
        });
        view.findViewById(R.id.ll_pyquan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> params3 = new HashMap<>();
                Map<String, String> params4 = new HashMap<>();
                params3.put("req_bd", params4);
                HttpClient.post(GeneralizeActivity.this, Constants.SHARE, params3, new CallBack<ProListBean>() {
                    @Override
                    public void onSuccess(ProListBean resultBean) {
                        String url =  resultBean.share_url;
                        Platform platform = ShareSDK.getPlatform(WechatMoments.NAME);
                        Platform.ShareParams shareParams = new  Platform.ShareParams();
                        shareParams.setTitle(resultBean.share_title);
                        shareParams.setText(resultBean.share_desc);
                        shareParams.setImageUrl(resultBean.share_icon);
                        shareParams.setUrl(url);
                        //分享网页
                        shareParams.setShareType(Platform.SHARE_WEBPAGE);
                        platform.share(shareParams);
                    }
                });
            }
        });
        view.findViewById(R.id.ll_weibo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> params5 = new HashMap<>();
                Map<String, String> params6 = new HashMap<>();
                params5.put("req_bd", params6);
                HttpClient.post(GeneralizeActivity.this, Constants.SHARE, params5, new CallBack<ProListBean>() {
                    @Override
                    public void onSuccess(ProListBean resultBean) {
                        String url =  resultBean.share_url;
                        Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
                        Platform.ShareParams shareParams = new  Platform.ShareParams();
                        shareParams.setTitle(resultBean.share_title);
                        shareParams.setText(resultBean.share_desc);
                        shareParams.setImageUrl(resultBean.share_icon);
                        shareParams.setUrl(url);
                        //分享网页
                        shareParams.setShareType(Platform.SHARE_WEBPAGE);
                        platform.share(shareParams);
                    }
                });
            }
        });

        View rootView = LayoutInflater.from(this).inflate(R.layout.activity_login, null);
        mPopWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
        mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1);
            }
        });
    }

    private void setBackgroundAlpha(float v) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = v;
        getWindow().setAttributes(lp);
    }
}